package com.apache.log.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.audit.entity.LogModel;
import com.apache.audit.entity.LogModelDetail;
import com.apache.audit.entity.LogOperation;
import com.apache.database.model.Page;
import com.apache.log.common.LoadClassUtil;
import com.apache.log.manager.UnityBaseManager;
import com.apache.tools.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * @description: 对外统一接口实现
 * @author: Hou Dayu
 * @date: Created in 2020/11/18
 */
@Service("logDymicSqlPlugin")
public class LogDymicSqlPluginImpl implements PluginConnector {

    @Autowired
    private UnityBaseManager unityBaseManager;

    @Override
    public Object execute(ParamsVo param) throws Exception {
        ResultEntity entity = new ResultEntity();
        entity.setResult("true");
        String objName = "";
        String modelTypes = param.getParamsToStr("modelTypes");
        String exeType = "s";
        if(StrUtil.isNotNull(modelTypes)) {
            String str[] = modelTypes.split("_");
            exeType = str[0];
            objName = str[1];
        }
        String keyPrefix = StrUtil.isNull(objName)?"":objName+".";
        switch(objName){
            case "logModel":
                LogModel logModel = new LogModel();
                logModel = (LogModel) LoadClassUtil.getInstance().convert2Bean(param.getParams(),logModel,keyPrefix);
                param.setObj(logModel);
                if(exeType.startsWith("d")){
                    param.setInfoId(logModel.getModelId());
                }
                break;
            case "logModelDetail":
                LogModelDetail detail = new LogModelDetail();
                detail = (LogModelDetail) LoadClassUtil.getInstance().convert2Bean(param.getParams(),detail,keyPrefix);
                param.setObj(detail);
                if(exeType.startsWith("d")){
                    param.setInfoId(detail.getDetailId());
                }
                break;
            case "logOperation":
                LogOperation operation = new LogOperation();
                operation = (LogOperation) LoadClassUtil.getInstance().convert2Bean(param.getParams(),operation,keyPrefix);
                param.setObj(operation);
                if(exeType.startsWith("d")){
                    param.setInfoId(operation.getLogId());
                }
                break;
        }
        if(exeType.startsWith("i")){
            String id = unityBaseManager.saveInfo(param);
            entity.setMessage("操作成功");
            entity.setEntity(id);
        } else if(exeType.startsWith("u")){
            boolean flag = unityBaseManager.editInfo(param);
            entity.setMessage(flag?"修改成功":"修改失败");
            entity.setEntity(flag);
        } else if(exeType.startsWith("d")){
            boolean flag = unityBaseManager.deleteInfo(param);
            entity.setMessage(flag?"删除成功":"删除失败");
            entity.setEntity(flag);
        } else {
            param.setParams("pageIndex",param.getParams("page"));
            param.setParams("pageSize",param.getParams("rows"));
            Page page = unityBaseManager.getPageInfo(param);
            entity.setMessage("获取成功");
            entity.setEntity(page);
        }
        return entity;
    }
}
